package com.yanq.liancar.activity;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.bean.SignInBean;
import com.yanq.liancar.bean.SignInListBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.Utils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.aigestudio.datepicker.bizs.calendars.DPCManager;
import cn.aigestudio.datepicker.bizs.decors.DPDecor;
import cn.aigestudio.datepicker.cons.DPMode;
import cn.aigestudio.datepicker.views.DatePicker;

public class SigInRecordActivity extends BaseToolbarActivity {


    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_daynum)
    TextView tvDaynum;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.datepicker)
    DatePicker datepicker;
    private Calendar mCalendar;
    private List<String> dateList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.setStatusBar(this, false, true);
        setContentView(R.layout.activity_siginrecord);
        ButterKnife.bind(this);

        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int date = c.get(Calendar.DATE);
        LogUtils.e("dadadada",dateList.size()+"");

        getSignIn();
        dateList=getIntent().getExtras().getStringArrayList("dateList");
        LogUtils.e("dadadada",dateList.size()+"");
        mCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        LogUtils.e("111111111", mCalendar.get(Calendar.YEAR) + "=====" + mCalendar.get(Calendar.MONTH));
        datepicker.setDate(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH) + 1);
        DPCManager.getInstance().setDecorBG(dateList);
        datepicker.setMode(DPMode.SINGLE);
        datepicker.setDPDecor(new DPDecor() {
            @Override
            public void drawDecorBG(Canvas canvas, Rect rect, Paint paint) {
                paint.setColor(getResources().getColor(R.color.light_blue_color));
                canvas.drawCircle(rect.centerX(), rect.centerY(), rect.width() / 2F, paint);
            }
        });
        getLastTime();
    }

    @OnClick(R.id.img_back)
    public void onViewClicked() {
        finish();
    }

    private void getSignIn() {
        OkGo.<String>get(Config.s + "llc/querySignRecord")
                .tag(this)
                .params("token", UserBean.getcToken(this))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        SignInListBean bean = gson.fromJson(response.body(), SignInListBean.class);
                        if (bean.isIsSuccess()) {
                            dateList.clear();
                            dateList.addAll(bean.getData());

                        } else {
                            RxToast.error(bean.getOutputMessage());
                        }
                    }

                });
    }

    private void getLastTime() {
        //获取上次签到时间与连续签到次数
        OkGo.<String>get(Config.s + "llc/getLastSignMsg")
                .tag(this)
                .params("token", UserBean.getcToken(this))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        SignInBean info = gson.fromJson(response.body(), SignInBean.class);
                        if (info.isIsSuccess()) {
                            tvDaynum.setText(info.getData().getSignNum() + "天");
                        } else {
                            RxToast.error(info.getOutputMessage());
                        }
                    }
                });
    }

}
